Repository navigation
piece_counts: vectorised (#69) - #568
Merged
Merged
Conversation
piece_counts sorted every pixel (np.unique with return_inverse) and labelled each label again in its bounding box. Each row is now cut into runs of one label, and runs of one label in neighbouring rows that touch (8-connected, from masks.binary.STRUCTURE) are joined with scipy.sparse.csgraph.connected_components. New pure module mask_pieces; mask_scan.piece_counts is the same function, imported. Results are byte-identical: a property test compares it with the former function on random masks of every label pixel type and on the benchmark masks, and every digest of benchmark_kernels --group masks is unchanged. 4096 px cells: 2.37 s -> 0.18 s; one position's scan 5.2 s -> 1.25 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
) The runs route of the first version needed up to 3.4 GiB and 7.4 s on a 4096 px two-label checkerboard (464 MiB and 0.93 s before), as runs are almost pixels there and its int64 temporaries were all alive at the graph. Now: - runs are found without a sort: places are row * (columns + 2) + column, increasing, so the runs above a run that touch it are one slice found by two binary searches; places and edges are int32 when they fit (bounds checked), int64 otherwise; temporaries are freed before the graph is built, and the label bound is no longer recomputed; - above RUNS_PER_PIXEL_LIMIT = 0.25 runs per pixel the former route (sort, then ndimage.label per label in its box) is taken, unchanged. Every identity test now runs three times: runs forced, boxes forced and the route chosen. The kernel benchmark gains three pixel-scale masks (two checkerboards and noise); docs/benchmarks.md has before, first version and after, with equal digests, and the trade-off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
SciPy's failure inside piece_counts was the only guard against a plane of height or width 0; with the runs route it has no labels, so it counted as an image and as a plane of at most one label in its mask type's kind. scan_masks now refuses it, naming the mask (mask_without_pixels). Regression test, user guide, contract §15 and changelog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
A mask of shape (1, 2**31 - 3) chose int32 places (rows * width is the int32 maximum) but the search subtracts width + reach, 2**31, and raised OverflowError. The place type is now chosen by _place_kind, which also counts the reach. Test of the selection only, no large allocation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
guijacquemet
marked this pull request as ready for review
November 26, 2026 06:31
guijacquemet
pushed a commit
that referenced
this pull request
Nov 26, 2026
…0.9.0 release branch One conflict, in CHANGELOG.md: both sides added entries at the top of the v0.9.0 list. Both kept; the #576 entry is in the v0.9.0 section. main is not merged: #568, #572 and the later work stay out of v0.9.0. The notebooks' pins, the RELEASE_CALLS entry and the pin assertion are put back to their pre-pin values (the branch carried commit B's) so that commit B' moves them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Post-v0.9.0: not for the release. Part of #69 (the
piece_countsitem only;pair_countsand the K(K−1) repeated pair work of #69 are untouched, so this does not close it).Summary
mask_pieces.py(new, pure):piece_countstakes one of two routes, with the same counts either way.row * (columns + 2) + column, and places increase in row-major order, so the runs of the row above whose widened columns overlap a run are one slice, found by twosearchsortedcalls. No sort is needed. Of those, the runs with the same label are joined withscipy.sparse.csgraph.connected_components, and a label's pieces are its groups of runs. The corner reach (8-connected) comes frommasks.binary.STRUCTUREand is not restated.RUNS_PER_PIXEL_LIMIT= 0.25 runs per pixel, the former function is used unchanged (a sort of every pixel, thenndimage.labelper label in its bounding box).int32when every value and every search fits (bounds checked explicitly,_place_kind) andint64otherwise. Temporaries are deleted before the graph is built. NumPy/SciPy only, no new dependency.mask_scan.py:piece_countsis imported frommask_pieces(it was already at the 600-line limit). New:scan_masksrefuses a mask plane of no pixels (mask_without_pixels, naming the mask).tools/benchmark_kernels.py --group masksgains three pixel-scale masks (benchmark_inputs.pixel_mask: a two-labeluint16checkerboard, a one-labelint32checkerboard, and half the pixels carrying labels 1–4).docs/benchmarks.md(before / first version / after, and the trade-off),CHANGELOG.md,AGENTS.mdmap,docs/development.md, user guide message table and contract §15 (mask_without_pixels),pyproject.tomllayer (mask_pieces= pure). Reports:docs/benchmark_results/2026-11-26-kernels-masks-{before,first,after}-69.json.Review of
85715dc(CHANGES NEEDED): fixesint32, and above 0.25 runs per pixel the former route is taken. The pixel-scale cases now cost what they cost onmain(table below). The first version needed 3.4 GiB on the two-label checkerboard; it now needs 464 MiB, the same asmain.scan_masksnow raisesValueError"the mask … has no pixels (shape (0, 8)); save the mask with the image's height and width (mask_without_pixels)". Regression testtest_a_mask_plane_without_pixels_is_refused(0×8 image and masks throughpreflightandscan_masks). It failed (did not raise) before the fix. Documented in the CHANGELOG, user guide and contract.int(label.max()) + 1computed three times. Gone: the new search has no label bound.Review of
5986acb(MERGE on the code; one low finding): fixint32places (rows * widthwas exactly the int32 maximum), but the search subtractswidth + reach= 2³¹ and raisedOverflowError. The place type is now chosen by_place_kind(rows, columns, reach):int32only whenrows * (columns + 2) + reachfits (b2c8e46).test_places_are_int32_only_when_every_search_fitstests the selection function only, with no large allocation: the boundary shapes with reach 1 and 0, a two-row case, and 4096². It fails with the former condition.mainat316be56(Run: show every check before the first image read, and make the header check linear (v0.9.0 release blocker) #567) is merged in (41c1bcf). The only conflict wasCHANGELOG.md, where both entries are kept.Identity evidence (byte-identical)
tests/test_mask_pieces.pykeeps the former function word for word as the reference, and comparesdtype,shapeandtobytes()of both outputs. Every test runs three times (an autouse fixture): runs route forced, boxes route forced, and the routepiece_countschooses. The tests cover:bool,uint8,int8,uint16,int16,uint32,int32,int64,uint64): touching labels with holes, scattered single pixels, blocks with repeated labels and holes, no background, a one-label checkerboard touching at corners and edges, and empty masks;b2c8e46: 75 passed, 12 skipped. The skips are the route-choice and place-type tests under the two forced routes; neither depends on the route.Benchmark (#544's tool with the new cases, same machine, five runs in turn: before, after, first, before, after; 7 timed calls after a warm-up)
Intel Xeon @ 2.10 GHz, 4 CPUs, Python 3.13.18, numpy 2.5.5, scipy 1.18.1. Another job's tests were running (1-minute load 1.9–2.1 at the starts), shared by every run. Before =
mainat88a1c42, first = this PR at85715dc, after =5986acbsource. The source was chosen withPYTHONPATH. (b2c8e46only changes which integer type is chosen at 2³¹ places, which no benchmark mask reaches.)piece_countscells 20484abd144b846d176cpiece_countscells 4096c575615afca962cf69de16bd28281cc3ec5910c05e04c1c7e90ded1a1d3176d96543cf267d7ad990484c7df9ddc9e95de90ded1a1d3176d96543cf267d7ad99099f3f03132d213f6pair_countsis unchanged: 241–259 ms at 4096 in every run. Peak RSS of the benchmark process: before 905 / 891 MiB, after 889 / 889 MiB, first 3,720 MiB.Trade-off, stated plainly. The runs route costs per run; the former route costs per pixel (about 32 bytes each for the sort). The threshold sweep below was run at 4096 px outside the tool (median of 3,
tracemallocpeak, equal outputs). Each cell shows the runs route first, then the former route.So the limit is 0.25. Below it, the runs route never used more memory than the former one and was at most about 10 % slower (at the limit itself). Above it, the route is the former one. The extra cost of choosing is one boolean plane (16 MiB at 4096²), freed before the former route starts. A mask just below the limit that the runs route handles badly in time is possible but was not found.
Adversarial check outside the tool (former function vs new, median of 3, equal outputs):
Behaviour outside the former results
piece_countsof a mask with a side of length 0 returns no labels (the former function failed inside SciPy). The mask scan now refuses such a plane explicitly (fix 2).ValueError. Before, SciPy raisedRuntimeError, or returned no labels for an all-background mask.Notes for review
machine.commitis the tool's checkout (85715dc), not the source measured, which was set withPYTHONPATH(stated indocs/benchmarks.md).26c9404,5986acb,b2c8e46), andmainis merged in rather than rebased.Tests
b2c8e46(test_mask_pieces,test_mask_scan,test_masks,test_benchmark_kernels): 156 passed, 12 skipped; ruff and--rulespass. The full check was not rerun afterb2c8e46: the release has the lock, and CI runs on the push.5986acb(the same files plus the docs/API/contract/guide tests): 525 passed, 3 skipped.5986acb: the same commandstools/check.pyruns, with pytest given--basetempin the session scratchpad (noPYTEST_ADDOPTS).ruff check .passed,ruff format --check .passed (762 files), pytest 8794 passed, 22 skipped, code rules (--rules) passed, exit 0. The 22 skips:skimage(tissue-mask);🤖 Generated with Claude Code
https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a